<template>
  <div class="channel-edit">
    <!-- 我的频道 -->
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>

    <div class="badge">
      <van-badge v-for="(channelItem, index) in mychannels" :key="index">
        <div
          class="text"
          :class="{ active: active === index }"
          @click="onMyChannelClick(channelItem, index)"
        >
          {{ channelItem.name }}
        </div>
        <template #content>
          <van-icon
            name="close"
            class="badge-icon"
            v-if="isEdit && index !== 0"
          />
        </template>
      </van-badge>
    </div>

    <!-- 频道推荐 -->
    <van-cell title="频道推荐" :border="false"></van-cell>

    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="channel in recommendChannels"
        :key="channel.id"
        :text="channel.name"
        @click="addChannelFn(channel)"
        icon="plus"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannelsAPI,
  addUserChannelAPI,
  deleteUserChannelAPI
} from '@/api'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'

export default {
  props: {
    mychannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  computed: {
    recommendChannels () {
      return this.allChannels.filter(channel => {
        const mychannel = this.mychannels.find(myChannel => {
          return myChannel.id === channel.id
        })
        return !mychannel
      })
    },
    ...mapState(['user', 'channelsKey'])
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    // 加载所有频道
    async loadAllChannels () {
      try {
        const { data } = await getAllChannelsAPI()
        this.allChannels = data.data.channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    async addChannelFn (chan) {
      this.mychannels.push(chan)
      if (this.user) {
        // 已登录
        try {
          // 已登录，数据存储到线上
          await addUserChannelAPI({
            id: chan.id, // 频道 id
            seq: this.mychannels.length // 频道的 序号
          })
          this.$toast('添加成功')
        } catch (err) {
          this.$toast('保存失败')
        }
      } else {
        // 未登陆
        setItem(this.channelsKey, this.mychannels)
      }
    },
    //
    onMyChannelClick (chan, ind) {
      if (this.isEdit) {
        // 执行删除操作
        if (ind === 0) return
        if (ind <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        this.mychannels.splice(ind, 1)
        // 删除数据持久化
        this.deleteChannel(chan)
      } else {
        // 执行跳转操作
        this.$emit('update-active', ind, false)
      }
    },
    // 删除渠道
    async deleteChannel (channel) {
      try {
        if (this.user) {
          // 已登录，将数据存储到线上
          await deleteUserChannelAPI(channel.id)
        } else {
          // 未登录，将数据存储到本地
          setItem(this.channelsKey, this.mychannels)
        }
      } catch (err) {
        console.log(err)
        this.$toast('删除频道失败，请稍后重试')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  .badge {
    display: flex;
    justify-content: flex-start;
    align-content: flex-start;
    flex-wrap: wrap;
    padding: 10px;

    .van-badge__wrapper {
      height: 86px;
      line-height: 86px;
      min-width: 165px;
      box-sizing: border-box;
      margin: 8px;
      text-align: center;
      background-color: #f6f6f6;
      .text {
        color: #222;
        font-size: 24px;
      }

      .active {
        color: red;
      }

      /deep/ .van-badge {
        background-color: unset;
        padding: unset;
        border: unset;
        color: red;
        font-size: 20px;

        .badge-icon {
          display: block;
          font-size: 16px;
          line-height: 16px;
        }
      }
    }
  }

  /deep/ .recommend-grid {
    .van-grid-item__content {
      background-color: #f6f6f6;
      flex-direction: row;
      color: #222;
      .van-icon {
        font-size: 12px;
      }

      .van-grid-item__text {
        font-size: 24px;
        margin-top: 0;
      }
    }
  }
}
</style>
